package com.example.healkep.View.health.helper;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.example.healkep.utils.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

public class healthHelper {
    public static int SUCCESS = 1;
    public static int FAILED = 2;
    public static int CONNECT_FAILED = 3;
    private Handler handler;
    public healthHelper(Handler handler){
        this.handler = handler;
    }
    public void getDataByStr(String str){
        new Thread(()->{
            Gson gson = new Gson();
            Map<String,String> map = new HashMap<>();
            map.put("name",str==null?"":str);
            MediaType mediaType = MediaType.parse("application/json; charset=UTF-8");
            RequestBody requestBody = RequestBody.create(gson.toJson(map),mediaType);
            try{
                Response response = HttpUtil.post("/diseases/get",requestBody);
                if(response.code()!=200){
                    handler.sendEmptyMessage(CONNECT_FAILED);
                }else{
                    String json = response.body().string();
                    if(json.length()<5){
                        handler.sendEmptyMessage(FAILED);
                    }else{
                        List<String> lst = new ArrayList<>();
                        JsonArray array = gson.fromJson(json,JsonArray.class);
                        for(int i = 0;i<array.size();i++){
                            if(array.get(i).isJsonObject()){
                                JsonObject jsonObject = array.get(i).getAsJsonObject();
                                if(jsonObject.has("name")){
                                    lst.add(jsonObject.get("name").getAsString());
                                }
                            }else{
                                handler.sendEmptyMessage(FAILED);
                                return;
                            }
                        }
                        Message msg = new Message();
                        msg.what = SUCCESS;
                        Bundle bundle = new Bundle();
                        bundle.putStringArrayList("diseases", (ArrayList<String>) lst);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    }
                }
            }catch (IOException e){
                handler.sendEmptyMessage(CONNECT_FAILED);
            }
        }).start();
    }

    public void getUserDiseases(String username){
        new Thread(()->{
            Gson gson = new Gson();
            Map<String,String> map = new HashMap<>();
            map.put("username",username);
            MediaType mediaType = MediaType.parse("application/json; charset=UTF-8");
            RequestBody requestBody = RequestBody.create(gson.toJson(map),mediaType);
            try {
                Response response = HttpUtil.post("/information/get",requestBody);
                if(response.code()!=200){
                    handler.sendEmptyMessage(CONNECT_FAILED);
                }else{
                    String str = response.body().string();
                    if(str.length()<5){
                        handler.sendEmptyMessage(FAILED);
                    }else{
                        JsonObject jsonObject = gson.fromJson(str,JsonObject.class);
                        if(jsonObject.has("diseases")){
                            JsonElement element = jsonObject.get("diseases");
                            Message msg = new Message();
                            msg.what = SUCCESS;
                            Bundle bundle = new Bundle();
                            bundle.putString("disease",element.getAsString());
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }else{
                            handler.sendEmptyMessage(FAILED);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public void updateDisease(String username,String diseases){
        new Thread(()->{
            Gson gson = new Gson();
            Map<String,String> map = new HashMap<>();
            map.put("username",username);
            map.put("diseases",diseases);
            MediaType mediaType = MediaType.parse("application/json; charset=UTF-8");
            RequestBody requestBody = RequestBody.create(gson.toJson(map),mediaType);
            try {
                Response response = HttpUtil.post("/information/updatedis",requestBody);

                if(response.code()!=200){
                    handler.sendEmptyMessage(CONNECT_FAILED);
                }else{
                    Message msg = new Message();
                    msg.what = SUCCESS;
                    Bundle bundle = new Bundle();
                    bundle.putString("disease",diseases);
                    msg.setData(bundle);
                    handler.sendMessage(msg);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
